package com.crazymo.moskin.core;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * @author : Crazy.Mo
 */
public class Util {
    public static class SharePreference {
        public static void save(Context context, String preferenceName, String key, Object object) {
            SharedPreferences.Editor editor = context.getSharedPreferences(preferenceName, 0).edit();
            if (object instanceof String) {
                editor.putString(key, (String) object);
            } else if (object instanceof Integer) {
                editor.putInt(key, (Integer) object);
            } else if (object instanceof Boolean) {
                editor.putBoolean(key, (Boolean) object);
            } else if (object instanceof Float) {
                editor.putFloat(key, (Float) object);
            } else if (object instanceof Long) {
                editor.putLong(key, (Long) object);
            } else {
                editor.putString(key, object.toString());
            }
            editor.apply();
        }

        public static Object get(Context context, String preferenceName, String key, Object defObject) {
            SharedPreferences sharedPreferences = context.getSharedPreferences(preferenceName, 0);
            if (defObject instanceof String) {
                return sharedPreferences.getString(key, (String) defObject);
            }
            if (defObject instanceof Integer) {
                return sharedPreferences.getInt(key, (Integer) defObject);
            }
            if (defObject instanceof Boolean) {
                return sharedPreferences.getBoolean(key, (Boolean) defObject);
            }
            if (defObject instanceof Float) {
                return sharedPreferences.getFloat(key, (Float) defObject);
            }
            if (defObject instanceof Long) {
                return sharedPreferences.getLong(key, (Long) defObject);
            }
            return sharedPreferences.getString(key, defObject.toString());
        }
    }
}
